package com.meitianhui.convenitenbuy.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.store.adapter.StorePopAdapter;
import com.meitianhui.convenitenbuy.module.store.model.StoreType;

import java.util.List;

/**
 * Created by Administrator on 2017/11/22.
 */

public class StorePopWindow extends PopupWindow {
    private Context mContext;
    private RecyclerView mRecyclerView;
    private StorePopAdapter mAdapter;
    private List<StoreType> mStoreTypeList;
    private OnItemClickListener mOnClick;
    private StorePopWindow mPop;

    private View mView;
    public StorePopWindow(Context context) {
        super(context);
    }


    public StorePopWindow(Context context,View view,List<StoreType> storeTypeList,OnItemClickListener onClick) {
        super(context);
        this.mContext = context;
        mStoreTypeList = storeTypeList;
        mOnClick = onClick;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.pop_store, null);
        mPop = new StorePopWindow(context);
        initView();
        mPop.setContentView(mView);
        mPop.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        mPop.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        mPop.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        mPop.setBackgroundDrawable(dw);
        mPop.getBackground().setAlpha(150);
        mPop.showAsDropDown(view);

    }

    protected void initView() {

        mRecyclerView = (RecyclerView) mView.findViewById(R.id.rv_pop);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new StorePopAdapter(mStoreTypeList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(mOnClick!=null){
                    mOnClick.onClick(mStoreTypeList.get(position).getIndex(),mStoreTypeList.get(position).getContent());
                    closePopWindow();
                }
            }
        });

    }

    public void closePopWindow() {
        if (mPop != null) {
            mPop.dismiss();
        }

    }


    public interface OnItemClickListener {
        void onClick(String id,String content);
    }
}


